import http from '@/utils/http'
// jwt获取用户名和公司Id
export const jwtUserName = (params) => {
  return http.post(`${window.SITE_CONFIG['sitURL']}/front/sso/jwt/username`, {
    id_token: params.idToken,
    publicKey: params.publicKey
  })
}
export const getJwtToken = (params) => {
  return http.post(`${window.SITE_CONFIG['sitURL']}/auth/jwt`, {
    grant_type: 'sso',
    appCode: 'CBRAIN',
    client_id: 'CBRAIN',
    id_token: params.idToken,
    publicKey: params.publicKey,
    username: params.username
  })
}

export const loginUser = (params) => {
  return http.post(`${window.SITE_CONFIG['sitURL']}/auth/login`, params)
}

export const getUserInfo = (params) => {
  return http.get(`${window.SITE_CONFIG['sitURL']}/front/consumer/usercenter/custUserCenterInfo`, {
    params: {
      appCode: 'PLANTMATE_OFFICIAL',
      companyId: ''
    }
  })
}
export const RemoveCookie = (cookieName) => {
  var cookies = document.cookie.split(";")//将所有cookie键值对通过分号分割为数组

  //循环遍历所有cookie键值对
  for (var i = 0; i < cookies.length; i++) {
      //有些cookie键值对前面会莫名其妙产生一个空格，将空格去掉
      if (cookies[i].indexOf(' ') == 0) {
          cookies[i] = cookies[i].substring(1)
      }

      //比较每个cookie的名称，找到要删除的那个cookie键值对
      if (cookies[i].indexOf(cookieName) == 0) {
          var exp = new Date()//获取客户端本地当前系统时间
          exp.setTime(exp.getTime() - 60 * 1000)//将exp设置为客户端本地时间1分钟以前，将exp赋值给cookie作为过期时间后，就表示该cookie已经过期了, 那么浏览器就会将其立刻删除掉

          document.cookie = cookies[i] + ";expires=" + exp.toUTCString()//设置要删除的cookie的过期时间，即在该cookie的键值对后面再添加一个expires键值对，并将上面的exp赋给expires作为值(注意expires的值必须为UTC或者GMT时间，不能用本地时间），那么浏览器就会将该cookie立刻删除掉
          //注意document.cookie的用法很巧妙，在对其进行赋值的时候是设置单个cookie的信息，但是获取document.cookie的值的时候是返回所有cookie的信息

          break//要删除的cookie已经在客户端被删除掉，跳出循环
      }
  }
}
export const buriedPoint = (params) => {
  function formatDateTime (value) {
    let now = new Date(value)
    let year = now.getFullYear() // 年
    let month = now.getMonth() + 1 // 月
    let day = now.getDate() // 日
    let hour = now.getHours() // 时
    let minutes = now.getMinutes() // 分
    let seconds = now.getSeconds() // 秒
    let clock = year + '-'
    if (month < 10) clock += '0'
    clock += month + '-'
    if (day < 10) clock += '0'
    // clock += day
    clock += day + ' '
    if (hour < 10) clock += '0'
    clock += hour + ':'
    if (minutes < 10) clock += '0'
    clock += minutes + ':'
    if (seconds < 10) clock += '0'
    clock += seconds
    return clock
  }
  let buriedPointInformation = JSON.parse(localStorage.getItem('buriedPointInformation'))
  let param = {
    ...buriedPointInformation,
    appName:params,//使用的应用名称
    appTabName:'',//使用应用tab名称
    useTime: formatDateTime(new Date()),//使用时间
  }
  return http.post(`/home/sys/userLoginLog/insert`, param)
  .then(({ data: res }) => {
    console.log(res.data);
  })
}